package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.model.main.City;
import com.xinghou.platform.model.main.Province;
import com.xinghou.platform.model.main.StoreImage;
import com.xinghou.platform.model.main.Test;

@Repository
public class TestDAOImpl {
	/*
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public Test getTestById(Integer id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select * from test where id = ?";
		Test test = jdbcTemplate.queryForObject(sql, new Object[] { id }, new TestMapper());
		CustomerContextHolder.clearCustomerType();
		return test;
	}

	@Override
	public Test updateTest(Test test) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		String sql = "update test set name = ? where id = ?";
		jdbcTemplate.update(sql, new Object[] { test.getName(), test.getId() });
		CustomerContextHolder.clearCustomerType();
		return test;
	}
	
	@Override
	public void saveTest(Test test) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("INSERT INTO test(name) VALUES (?)");
		
		Object[] params = {test.getName()};
		
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();

	}
	
	private static final class TestMapper implements RowMapper<Test> {
		public Test mapRow(ResultSet rs, int rowNum) throws SQLException {
			Test test = new Test();
			test.setId(rs.getInt("id"));
			test.setName(rs.getString("name"));
			return test;
		}
	}
	
	private static final class ProvinceMapper implements RowMapper<Province> {
		public Province mapRow(ResultSet rs, int rowNum) throws SQLException {
			Province province = new Province();
			province.setCode(rs.getString("code"));
			province.setName(rs.getString("name"));
			return province;
		}
	}
	
	private static final class CityMapper implements RowMapper<City> {
		public City mapRow(ResultSet rs, int rowNum) throws SQLException {
			City city = new City();
			city.setCode(rs.getString("code"));
			city.setName(rs.getString("name"));
			city.setPcode(rs.getString("pcode"));
			return city;
		}
	}

	private static final class StringMapper implements RowMapper<String> {
		public String mapRow(ResultSet rs, int rowNum) throws SQLException {
			return rs.getString("name");
		}
	}
	
	private static final class StoreImageMapper implements RowMapper<StoreImage> {
		public StoreImage mapRow(ResultSet rs, int rowNum) throws SQLException {
			StoreImage storeImage = new StoreImage();
			storeImage.setDescription(rs.getString("Description"));
			storeImage.setFileName(rs.getString("File_name"));
			storeImage.setImageId(rs.getInt("Image_id"));
			storeImage.setStoreId(rs.getInt("Image_type_id"));
			storeImage.setUri(rs.getString("uri"));
			return storeImage;
		}
	}
	
	@Override
	public List<String> getCityNames(String pcode) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select name from city where pcode = ?";
		List<String> list = jdbcTemplate.query(sql,new Object[] { pcode }, new StringMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	@Override
	public List<Province> getProvince()
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select * from province";
		List<Province> list = jdbcTemplate.query(sql,new ProvinceMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	@Override
	public List<City> getCitys(String pcode)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select * from city where pcode = ?";
		List<City> list = jdbcTemplate.query(sql,new Object[] { pcode }, new CityMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	@Override
	public void updateStoreImage(String name, String fileName) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "update store_image set File_name = ? where File_name = ?";
		jdbcTemplate.update(sql,new Object[] {fileName, name });
		CustomerContextHolder.clearCustomerType();
	}*/
}